import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:队列
 * User: LXW
 * Date: 2022-02-24
 * Time: 13:32
 */
class MyQueueOJ {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;

    public MyQueueOJ() {
        stack1 =  new Stack<>();
        stack2 =  new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(empty()) return-1;
        if(stack2.isEmpty()) {
            while(!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if(empty()) return-1;
        if(stack2.isEmpty()) {
            while(!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}
class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!qu1.isEmpty()) {
            qu1.offer(x);
        }else if(!qu2.isEmpty()) {
            qu2.offer(x);
        }else{
            qu1.offer(x);
        }
    }

    public int pop() {
        if(empty()) return -1;

        if(!qu1.isEmpty()) {
            int size = qu1.size();
            for(int i = 0; i < size-1;i++) {
                int val = qu1.poll();
                qu2.offer(val);
            }
            return qu1.poll();
        }

        if(!qu2.isEmpty()) {
            int size = qu2.size();
            for(int i = 0; i < size-1;i++) {
                int val = qu2.poll();
                qu1.offer(val);
            }
            return qu2.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()) return -1;

        if(!qu1.isEmpty()) {
            int val = -1;
            int size = qu1.size();
            for(int i = 0; i < size;i++) {
                val = qu1.poll();
                qu2.offer(val);
            }
            return val;
        }

        if(!qu2.isEmpty()) {
            int val = -1;
            int size = qu2.size();
            for(int i = 0; i < size;i++) {
                val = qu2.poll();
                qu1.offer(val);
            }
            return val;
        }
        return -1;
    }

    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}

public class Test {

}
